'''
Created on 16-07-2013

@author: Lukasz Rytel
'''
import networkx as nx
import numpy.random as npr
from graph.Graph_properties import Graph_properties
from utils.utils_functions import factorial



class GraphStructure(object):
    '''
    Representation of graph structure.
    '''

    def __init__(self,params=None):
        if type(params) == type(Graph_properties()):
            self.properties = params
            print "is manual"
        else:
            print "is random"
            g_param = Graph_properties()
            self.properties = g_param
        self.set_graph()

    def set_name(self,label):
        self.properties.label = label
                
    def set_graph(self):
        self.graph = nx.Graph()
        N=nx.path_graph(self.properties.nodes)
        self.graph.add_nodes_from(N)
        self.gen_edges()
        
    def gen_edges(self,weight=False):
        edges_arr = []
        max_iter = 2 * factorial(self.properties.nodes)
        iter = 0
        while (self.graph.number_of_edges() < self.properties.edges and iter < max_iter):
            msg_iter(iter,max_iter)
            i = npr.random_integers(0,self.properties.nodes)
            j = npr.random_integers(0,self.properties.nodes)
            if i != j:
                if weight == True:
                    w = npr.ranf()
                else:
                    w = 1
                ed = (i,j,w)
                edges_arr.append(ed)
                self.graph.add_weighted_edges_from(edges_arr)
            iter = iter + 1
        
    def is_equel(self,another_graph):
        # TO DO
        print "Graph '{0}' is the same like graph '{1}'".format(self.properties.label, another_graph.properties.label)
        
    def print_info(self):
        print "\n#### Info about : "
        print "Graph {0}:".format(self.properties.label)
        print "Number of nodes is {0}: {1}".format(self.graph.number_of_nodes(),self.graph.nodes())
        print "Number of edges is {0}:".format(self.graph.number_of_edges())
        for b in self.graph.adj.keys():
            for e in self.graph.adj[b].keys():
                print "({0},{1}) - {2}".format(b,e,self.graph.adj[b][e])
        print "#### End.\n"
        
def msg_iter(iter,im=0):
    if iter%100 == 0:
        print "..it is still running.. {0}/{1}".format(iter,im)